//---------------------------------------------------------------------------------
// Copyright (c) 2011, Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//---------------------------------------------------------------------------------


namespace Microsoft.Samples.MessagingWithTopics
{
    using System;
    using System.Collections.Generic;
    using Microsoft.ServiceBus;
    using Microsoft.ServiceBus.Messaging;

    public class Sender
    {
        private static string ServiceNamespace;
        private static string IssuerName;
        private static string IssuerKey;

        static void Main(string[] args)
        {
            GetUserCredentials();           
            TokenProvider tokenProvider =
                TokenProvider.CreateSharedSecretTokenProvider(IssuerName, IssuerKey);
            Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", ServiceNamespace, string.Empty);

            //*****************************************************************************************************
            //                                     Management Operations
            //*****************************************************************************************************            

            NamespaceManager namespaceManager = new NamespaceManager(serviceUri, tokenProvider);

            // Create a topic
            Console.WriteLine("\nCreating Topic 'IssueTrackingTopic'...");

            // Delete if exists
            if (namespaceManager.TopicExists("IssueTrackingTopic"))
            {
                namespaceManager.DeleteTopic("IssueTrackingTopic");
            }

            TopicDescription myTopic = namespaceManager.CreateTopic("IssueTrackingTopic");

            // Create a subscription
            Console.WriteLine("Creating Subscriptions 'AuditSubscription' and 'AgentSubscription'...");
            SubscriptionDescription myAuditSubscription = namespaceManager.CreateSubscription(myTopic.Path, "AuditSubscription");
            SubscriptionDescription myAgentSubscription = namespaceManager.CreateSubscription(myTopic.Path, "AgentSubscription");

            //*****************************************************************************************************
            //                                   Runtime Operations
            //*****************************************************************************************************

            MessagingFactory factory = MessagingFactory.Create(serviceUri, tokenProvider);            
            TopicClient myTopicClient = factory.CreateTopicClient(myTopic.Path);

            //*****************************************************************************************************
            //                                   Sending messages to a Topic
            //*****************************************************************************************************

            List<BrokeredMessage> messageList = new List<BrokeredMessage>();
            messageList.Add(CreateIssueMessage("1", "First message information"));
            messageList.Add(CreateIssueMessage("2", "Second message information"));
            messageList.Add(CreateIssueMessage("3", "Third message information"));

            Console.WriteLine("\nSending messages to topic...");

            foreach (BrokeredMessage message in messageList)
            {
                myTopicClient.Send(message);
                Console.WriteLine(
                    string.Format("Message sent: Id = {0}, Body = {1}", message.MessageId, message.GetBody<string>()));
            }

            Console.WriteLine("\nFinished sending messages, press ENTER to clean up and exit.");

            myTopicClient.Close();
            Console.ReadLine();
            namespaceManager.DeleteTopic("IssueTrackingTopic");
        }

        static void GetUserCredentials()
        {
            Console.Write("Please provide the service namespace to use: ");
            ServiceNamespace = Console.ReadLine();

            Console.Write("Please provide the issuer name to use: ");
            IssuerName = Console.ReadLine();

            Console.Write("Please provide the issuer key to use: ");
            IssuerKey = Console.ReadLine();
        }

        private static BrokeredMessage CreateIssueMessage(string issueId, string issueBody)
        {
            BrokeredMessage message = new BrokeredMessage(issueBody);
            message.MessageId = issueId;
            return message;
        }
    }
}
